﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ChannelVN.CMS.DynamicLayout.FrontEnd.Common
{
    public class WebHelpers
    {
        /// <summary>
        /// Registers the page client script
        /// </summary>
        /// <param name="page">Page to register</param>
        /// <param name="src">Script source</param>
        /// <returns>return True if registed, other else return False</returns>
        public static bool AddClientScript(Page page, string src)
        {
            var scriptFile = src.Substring(src.IndexOf("/") + 1);
            var clientScriptManager = page.ClientScript;
            if (clientScriptManager.IsClientScriptIncludeRegistered(scriptFile))
                return false; // already registered
            clientScriptManager.RegisterClientScriptInclude(scriptFile, src);
            return true;
        }

        /// <summary>
        /// Registers the page style sheet
        /// </summary>
        /// <param name="page">Page to register</param>
        /// <param name="src">Style sheet source</param>
        public static void AddClientCss(Page page, string src)
        {
            var css = new HtmlGenericControl("link");
            css.Attributes["type"] = "text/css";
            css.Attributes["rel"] = "stylesheet";
            css.Attributes["href"] = src;
            if (page.Header == null)
                return;
            page.Header.Controls.Add(css);
        }

        /// <summary>
        /// Add meta into page header
        /// </summary>
        /// <param name="page">Page object</param>
        /// <param name="tagName">Meta tag name</param>
        /// <param name="tagId">Meta tag id</param>
        /// <param name="attributes">List of attribute</param>
        public static void AddMetaTag(Page page, string tagName, string tagId, Dictionary<string, string> attributes)
        {
            var htmlHead = (null == page.Master ? page.Header : page.Master.Page.Header);

            if (htmlHead.FindControl(tagId) != null) return;
            var metaTag = new HtmlMeta { ID = tagId, Name = tagName };
            foreach (var key in attributes.Keys)
            {
                metaTag.Attributes.Add(key, attributes[key]);
            }
            htmlHead.Controls.Add(metaTag);
        }

        /// <summary>
        /// Add base meta tag
        /// </summary>
        /// <param name="page">Page object</param>
        /// <param name="baseUrl">Base host url</param>
        public static void AddBaseMetaTag(Page page, string baseUrl)
        {
            var htmlHead = (null == page.Master ? page.Header : page.Master.Page.Header);

            if (htmlHead.FindControl("BaseMetaTag") != null) return;
            var ltrBaseTag = new Literal {ID = "BaseMetaTag", Text = "<base href=\"" + baseUrl + "\" />"};
            htmlHead.Controls.Add(ltrBaseTag);
        }
    }
}
